package com.easylinkin.linkappapi.webcammanage.controller;

import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.webcammanage.entity.DataAnalystBean;
import com.easylinkin.linkappapi.webcammanage.entity.QueryDataCondition;
import com.easylinkin.linkappapi.webcammanage.service.DataAnalystESService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.thymeleaf.util.StringUtils;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 数据分析Controller
 */
@RestController
@RequestMapping("/dataAnalyst")
public class DataAnalystESController {

    @Autowired
    private DataAnalystESService dataAnalystESService;

    @PostMapping("selectDataAnalystPage")
    public RestMessage selectDataAnalystPage(@RequestBody RequestModel<QueryDataCondition> requestModel) {
        Assert.notNull(requestModel, "requestModel 不能为空");
        return RestBuilders.successBuilder().data(dataAnalystESService.selectDataAnalystPage(requestModel.getPage(), requestModel.getCustomQueryParams())).build();
    }

    @PostMapping("insertDataAnalystRecord")
    public RestMessage insertDataAnalystRecord(@RequestBody DataAnalystBean dataAnalystBean) {
        Assert.notNull(dataAnalystBean, "dataAnalystBean 不能为空");
        String[] result = dataAnalystESService.insertDataAnalystRecord(dataAnalystBean);
        if ("0".equals(result[0])) {
            if(result.length == 3){
                return RestBuilders.successBuilder().data(result[2]).build();
            }
            return RestBuilders.successBuilder().data(result[1]).build();
        }

        return RestBuilders.failureBuilder().message(result[1]).build();
    }

    @PostMapping("deleteDataAnalystRecord")
    public RestMessage deleteDataAnalystRecord(@RequestParam("id") String id) {
        Assert.isTrue(!StringUtils.isEmpty(id), "id 不能为空");
        return RestBuilders.successBuilder().data(dataAnalystESService.deleteDataAnalystRecord(id)).build();
    }

    @PostMapping("updateDataAnalystRecord")
    public RestMessage updateDataAnalystRecord(@RequestBody DataAnalystBean dataAnalystBean) {
        Assert.notNull(dataAnalystBean, "dataAnalystBean 不能为空");
        return RestBuilders.successBuilder().data(dataAnalystESService.updateDataAnalystRecord(dataAnalystBean)).build();
    }

    @PostMapping("importYYSHData")
    public RestMessage importYYSHData(@RequestParam(value = "file") MultipartFile file) {
        Assert.notNull(file, "文件不能为空");
        String[] result = dataAnalystESService.importYYSHData(file);
        if ("0".equals(result[0])) {
            return RestBuilders.successBuilder().data(result[1]).build();
        }

        return RestBuilders.failureBuilder().message(result[1]).build();
    }

    @PostMapping("exportYYSHData")
    public void exportYYSHData(@RequestBody QueryDataCondition queryParams, HttpServletRequest request, HttpServletResponse response) {
        dataAnalystESService.exportYYSHData(queryParams, request, response);
    }

    @GetMapping("exportTemplateFile")
    public void exportTemplateFile(HttpServletRequest request, HttpServletResponse response) {
        dataAnalystESService.exportYYSHTemplateFile(request, response);
    }

}
